{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "from os import listdir\n",
    "\n",
    "def cutLoc(hList,len):   #图片集分段，第几张图片另外开始。\n",
    "    count=0\n",
    "    no=0\n",
    "    cutLocation=[]\n",
    "    for i in hList:\n",
    "        count=count+i\n",
    "        if count>len:\n",
    "            cutLocation.append([no-1,count-i])\n",
    "            count=i\n",
    "        no=no+1\n",
    "    cutLocation.append([no-1,count])\n",
    "    return cutLocation\n",
    "\n",
    "def merger(imgList,position,num,resultPath):  #合并图片，图片集合，分段位置，结果编号，结果保存位置\n",
    "    width,height=imgList[0].size\n",
    "    for i in range(len(position)):\n",
    "        result = Image.new(imgList[0].mode,(width,position[i][1]))\n",
    "        if i==0:\n",
    "            h=0\n",
    "            for j in range(0,position[i][0]+1):\n",
    "                result.paste(imgList[j],box=(0,h))\n",
    "                imgW,imgH=imgList[j].size\n",
    "                h=h+imgH\n",
    "            result.save(resultPath+'\\\\'+'%s.jpg'%num)    \n",
    "        else:\n",
    "            h=0\n",
    "            for j in range(position[i-1][0]+1,position[i][0]+1):\n",
    "                result.paste(imgList[j],box=(0,h))\n",
    "                imgW,imgH=imgList[j].size\n",
    "                h=h+imgH\n",
    "            result.save(resultPath+'\\\\'+'%s.jpg'%num)  \n",
    "        num=num+1\n",
    "    return num\n",
    "\n",
    "def readImg(sourcePath,len): #读取图片，sourcePath图片位置，len，合并后一张图片最大长度\n",
    "    imgList = [Image.open(sourcePath+'\\\\'+fn)for fn in listdir(sourcePath)  if fn.endswith(\".jpg\")]       #  打开路径下的所有图片\n",
    "    hList=[]\n",
    "    for img in imgList:\n",
    "        width,height=img.size\n",
    "        hList.append(height)\n",
    "    position=cutLoc(hList,len)\n",
    "    return position, imgList\n",
    "\n",
    "sourcePath = r'D:\\q\\temp\\web'\n",
    "resultPath = r'D:\\q\\temp'\n",
    "num = 0\n",
    "\n",
    "#position, imgList=readImg(sourcePath,65000)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generateFolder(sourcePath):\n",
    "    folderList=[]\n",
    "    for i in range(0,218):\n",
    "        folder=sourcePath+'\\\\'+str(i)\n",
    "        folderList.append(folder)\n",
    "    return folderList\n",
    "sourcePathList=generateFolder(sourcePath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for source in sourcePathList:\n",
    "    position, imgList=readImg(source,65000)\n",
    "    print(position)\n",
    "    num=merger(imgList,position,num,resultPath)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
